mui.init();
var page = [0,0,0,0,0,0,0,0];
var totalPrice = 0;
(function($) {
  //阻尼系数
  var deceleration = mui.os.ios?0.003:0.0009;
  $('.mui-scroll-wrapper').scroll({
    bounce: false,
    indicators: true, //是否显示滚动条
    deceleration:deceleration
  });
  $.ready(function() {
    //循环初始化所有下拉刷新，上拉加载。
    $.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
      $(pullRefreshEl).pullToRefresh({
        down: {
          height:200,
          callback: function() {
            var self = this;
            var ul = self.element.querySelector('.mui-table-view');
            console.log(self.getAtrribute("id")+"--");
            ul.insertBefore(createFragment(ul, index, 10, true), ul.firstChild);
            self.endPullDownToRefresh();
          }
        },
        up: {
          auto: true,
          callback: function() {
            var self = this;
            var ul = self.element.querySelector('.mui-table-view');
            var ulId = ul.getAttribute("id");
            // console.log(ulId+"--");
            page[index]++;
            getData(ulId,page[index]);
            //ul.appendChild(createFragment(ul, index, 5));
            self.endPullUpToRefresh();
          }
        }
      });
    });

    document.getElementById("allSelect").onchange = function(){
      var checkbox = document.getElementsByClassName("selectBox");
      for(var i=0;i<checkbox.length;i++){
        if(this.checked){
          checkbox[i].checked=true;
        }else{
          checkbox[i].checked=false;
        }

        }
      compute();
    }

    document.getElementById("balance").onclick = function(){
      var price = new Array();
          price['包车服务'] = 0;
          price['场地'] = 0;
          price['场地布置'] = 0;
          price['达人服务'] = 0
          price['餐饮服务'] = 0;
      var orderNum = new Array();
      var checkbox = document.getElementsByClassName("selectBox");
      for(var i=0;i<checkbox.length;i++){
         if(checkbox[i].checked==true){
           orderNum.push(checkbox[i].value);
           var prices = checkbox[i].getAttribute("data-price");
           var num = checkbox[i].getAttribute("data-num");
           price[checkbox[i].getAttribute("data-type")] += parseFloat(prices) * parseFloat(num);
         }

      }

      var ordersNum = orderNum.join("|");
      //  console.log(ordersNum+"--"+totalPrice);
      //  console.log(price);
       if(ordersNum=="" || ordersNum==null){
         mui.toast("请选择商品");
         return ;
       }
       location.href = "/orders?ordersNum="+ordersNum+"&price="+totalPrice+"&car="+price['包车服务']+"&site="+price['场地']+"&layout="+price['场地布置']+"&person="+price['达人服务']+"&catering="+price['餐饮服务'];

    }



  });

  var getData= function (elementId,page){
      var associate = {
          'noPayList':{
            'tpl':'noPayTpl',
            'url':'/orders/list/'+page+'?ordersState=1&pageSize=100'
           },
          'noHandleList':{
            'tpl':'noHandleTpl',
            'url':'/orders/list/'+page+'?ordersState=2&pageSize=10'
          },
          'noUseList':{
            'tpl':'noUseTpl',
            'url':'/orders/list/'+page+'?ordersState=3&pageSize=10'
          },
          'usedList':{
            'tpl':'usedTpl',
            'url':'/orders/list/'+page+'?ordersState=4&pageSize=10'
          },
          'refundList':{
            'tpl':'refundTpl',
            'url':'/orders/list/'+page+'?ordersState=5&pageSize=10'
          },
          'cancelList':{
            'tpl':'cancelTpl',
            'url':'/orders/list/'+page+'?ordersState=6&pageSize=10'
          },
          'refundedList':{
            'tpl':'refundedTpl',
            'url':'/orders/list/'+page+'?ordersState=7&pageSize=10'
          },
          'refundfailList':{
            'tpl':'refundfailTpl',
            'url':'/orders/list/'+page+'?ordersState=8&pageSize=10'
          },
      };

    //  console.log(associate);
      var data;
      $.ajax(associate[elementId]['url'], {
        type: 'get',
        async: false,
        success: function(res) {
          for(var key in res.result){
            for(var i=0;i<res.result[key].length;i++){
              res.result[key][i].ctime = new Date(res.result[key][i].ctime).format('yyyy-MM-dd');
              res.result[key][i].ptype = key;
            }
          }
          var allTpl = template(associate[elementId]['tpl'],res);
          //console.log(document.getElementById(elementId)+"----");
          var element = document.getElementById(elementId);
          element.innerHTML = element.innerHTML+allTpl;

          if(elementId=="noPayList"){
            mui(".mui-checkbox").on("tap",".selectBox",function(e){
              this.checked = !this.checked;
                compute();
              e.stopPropagation();
            });
          }

        },
      });

  }

  function compute() {
     totalPrice = 0;

     var checkbox = document.getElementsByClassName("selectBox");
     for(var i=0;i<checkbox.length;i++){
       if(checkbox[i].checked==true){
         var price = checkbox[i].getAttribute("data-price");
         var num = checkbox[i].getAttribute("data-num");
         totalPrice += parseFloat(price) * parseFloat(num);
       }

      //  console.log(checkbox[i].getAttribute("data-price"));
     }
    totalPrice = totalPrice.toFixed(2);
    document.getElementById("totalPrice").innerHTML = totalPrice;
  }

})(mui);
